package com.thingsgrid.device.monitor.service.impl;
import com.mongodb.client.result.DeleteResult;
import com.thingsgrid.device.entity.PerformanceMetrics;
import com.thingsgrid.device.monitor.service.IMertricsService;

import com.thingsgrid.system.user.entity.User;
import com.thingsgrid.system.user.feign.IUserClient;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 指标管理
 */
@Service
public class MertricsServiceImpl implements IMertricsService {

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 调用user模块
	 */
	@Autowired
	private IUserClient userClient;


	/**
	 *
	 *查询指标名称
	 * @author 陈学栋
	 * @date 2019/8/23 13:55
	 */
	@Override
	public List<PerformanceMetrics> findMertricsList() {
		List<PerformanceMetrics> PerformanceMetricsList = mongoTemplate.find(new Query(),  PerformanceMetrics.class, "performance_metrics");
		return PerformanceMetricsList;
	}

	/**
	 *
	 *通过指标名称和指标类型查询列表
	 * @author 陈学栋
	 * @date 2019/8/23 14:08
	 */
	@Override
	public Map<String, Object> findListByNameAndType(String metricsName, String metricsType, Integer current, Integer size) {
		HashMap<String, Object> map = new HashMap<>();
		Query query = new Query().skip((current - 1) * size).limit(size);
		if(metricsName == null || metricsName.equals("") && metricsType == null || metricsType.equals("")){
			List<PerformanceMetrics> list = mongoTemplate.find(query, PerformanceMetrics.class, "performance_metrics");
			long pagecount = mongoTemplate.count(query, PerformanceMetrics.class, "performance_metrics");
			map.put("performanceMetrics",list);
			map.put("total",pagecount);
		} else {
			query.addCriteria(Criteria.where("metricsName").is(metricsName).and("metricsType").is(metricsType));
			List<PerformanceMetrics> list = mongoTemplate.find(query, PerformanceMetrics.class, "performance_metrics");
			long pagecount = mongoTemplate.count(query, PerformanceMetrics.class, "performance_metrics");
			map.put("performanceMetrics",list);
			map.put("total",pagecount);
		}
		return map;
	}
	/**
	 *
	 *添加指标管理
	 * @author 陈学栋
	 * @date 2019/9/6 14:13
	 */
	@Override
	public Boolean addMertrics(PerformanceMetrics performanceMetrics,Integer userId) {
		if(performanceMetrics.getMetricsName() != null && performanceMetrics.getMetricsName() != "" && performanceMetrics.getDescribe() != null
			&& performanceMetrics.getDescribe() != "" && performanceMetrics.getMetricsNote() != null && performanceMetrics.getMetricsNote() != ""
			&& performanceMetrics.getMetricsType() != null &&performanceMetrics.getMetricsType() !=""
			&& userId != null){
			String id = UUID.randomUUID().toString();
			PerformanceMetrics metrics = new PerformanceMetrics();
			metrics.setMetricsId(id);
			metrics.setMetricsName(performanceMetrics.getMetricsName());
			//数据总条数
			long count = mongoTemplate.count(new Query(), PerformanceMetrics.class, "performance_metrics");
			metrics.setMetricsNumber(++count);
			metrics.setMetricsName(performanceMetrics.getMetricsName());
			metrics.setProductId(performanceMetrics.getProductId());
			metrics.setMetricsCode(performanceMetrics.getMetricsCode());
			metrics.setMetricsType(performanceMetrics.getMetricsType());
			metrics.setDescribe(performanceMetrics.getDescribe());
			//创建时间
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(currentTime);
			metrics.setCreateTime(dateString);
			//通过userId查询用户信息
			Long nUserId = Long.valueOf(userId);
			R<User> user = userClient.userInfoById(nUserId);
			User data = user.getData();
			metrics.setCreateAuthor(data.getAccount());
			PerformanceMetrics performanceMetrics1 = mongoTemplate.save(metrics, "performance_metrics");
			if(performanceMetrics1.getMetricsId().equals(id)){
				return true;
			}
		}

		return false;
	}

	/**
	 *
	 *删除指标管理
	 * @author 陈学栋
	 * @date 2019/9/6 14:22
	 */
	@Override
	public Boolean deleteMertrics(String metricsIds) {

		String[] metricsId = metricsIds.split(",");
		Query query = null;
		//用于验证批量删除是否全部删除成功
		int j = 0;
		boolean flag = false;
		for (int i = 0; i < metricsId.length; i++){
			query = Query.query(Criteria.where("metricsId").is(metricsId[i]));
			DeleteResult performanceMetrics = mongoTemplate.remove(query, PerformanceMetrics.class, "performance_metrics");
			j += performanceMetrics.getDeletedCount();
		}
		if (j == metricsId.length){
			flag = true;
		}
		return flag;
	}
}
